<template>
  <div class="login">
    <el-form
      class="login-form"
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
    >
      <div class="title">登录页</div>
      <el-form-item prop="phone">
        <el-input
          placeholder="请输入账号"
          v-model="ruleForm.phone"
          size="large"
          type="text"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item prop="pass">
        <el-input
          placeholder="请输入密码"
          size="large"
          v-model="ruleForm.pass"
          type="password"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item>
        <el-button class="btn" type="primary" @click="submitForm(ruleFormRef)"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { user_login } from "../api/index";
import { useRouter } from "vue-router";
import { filterRoutes } from "../utils/authorization";
import { ElNotification } from "element-plus";
import "element-plus/es/components/notification/style/css";
import routes from '../router/routers'

var $router = useRouter();

var ruleForm = reactive({ phone: "", pass: "" });

var ruleFormRef = ref();

const validatePhone = (rule, value, callback) => {
  if (/^1[3-9]\d{9}$/.test(value) == false) {
    callback(new Error("请输入正确的手机号"));
  } else {
    callback();
  }
};
const validatePass = (rule, value, callback) => {
  if (/^\d{6}$/.test(value) == false) {
    callback(new Error("请输入符合要求的密码,必须是4位数字"));
  } else {
    callback();
  }
};

//表单, 表单项的验证规则
const rules = reactive({
  phone: [{ validator: validatePhone, trigger: "blur" }],
  pass: [{ validator: validatePass, trigger: "blur" }],
});

//点击"登录" 手动验证表单, 参数formEl是表单实例, validate是表单实例的方法, 用来验证整个表单.
const submitForm = (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    //validate验证通过valid为true,否则valid为false
    if (valid) {
      //发起登录请求
      var res = await user_login(ruleForm);
      if (res.data.code == 200) {
        //登录成功, 跳转首页, 保存token,userinfo
        //弹框
        ElNotification({
          title: '登录通知',
          message: res.data.msg,
          type: 'success',
        })
        //保存token,userinfo
        localStorage.setItem("token", res.data.token);
        localStorage.setItem("userinfo", JSON.stringify(res.data.userinfo));
        var accessRoutes = filterRoutes(
          routes,
          res.data.userinfo.role.permissions
        );
        console.log(accessRoutes);

        //将过滤出来的路由 动态添加到router中
        accessRoutes.forEach((route) => {
          $router.addRoute(route); //循环一次, 添加一条路由
        });
        // 跳转首页
        $router.push("/");
      } else {
        //弹框
        ElNotification({
          title: '登录通知',
          message: res.data.msg,
          type: 'error',
        })
      }
    } else {
      return false;
    }
  });
};
</script>

<style scoped lang='scss'>
.login {
  height: 100%;
  background-image: url(../assets/考勤管理-登录页-背景.png);
  display: flex;
  justify-content: center;
  align-items: center;
  .login-form {
    background-color: white;
    padding: 30px;
    min-width: 400px;
    border-radius: 10px;
    .title {
      margin-bottom: 30px;
      font-size: 20px;
      font-weight: bold;
      text-align: center;
    }
    .btn {
      margin: 30px auto;
    }
  }
}
</style>